package com.xzq.rongimtest;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 网络请求工具类
 */

public class HttpUtil {

    private static final String yuMing = "http://api.myas.maissy.net";

    private static final OkHttpClient client = new OkHttpClient();

    public static Response get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .get().build();
        return client.newCall(request).execute();
    }

    public static Response postWithHeaders(String url, RequestBody body) throws IOException, NoSuchAlgorithmException {
        String appKey = "pkfcgjstpoti8";
        String appSecret = "TxRlbPZscl";
        String nonce = DataUtil.getFixLengthString(18);
        long timeStamp = Calendar.getInstance().getTimeInMillis();
        String signature = DataUtil.sha1(appSecret + nonce + String.valueOf(timeStamp));
        Request request = new Request.Builder()
                .url(url)
                .addHeader("App-Key", appKey)
                .addHeader("Nonce", nonce)
                .addHeader("Timestamp", String.valueOf(timeStamp))
                .addHeader("Signature", signature)
                .post(body)
                .build();
        return client.newCall(request).execute();
    }
}
